#!/usr/bin/env jruby
# -*- coding: us-ascii -*-
# Elizabeth Wiethoff 2008

require 'java'
require 'test/unit'
dir = File.dirname(__FILE__)
require File.join(dir, '..', '..', 'lib', 'monkey', 'array2vector')

########################################

module Constants4Array2VectorTests
  STRING = 'hello'
  HASHTABLE = java.util.Hashtable.new
  
  ARRAY0 = []
  ARRAY3 = %w'foo bar baz'
  
  VECTOR0 = java.util.Vector.new(0)
  
  VECTOR3 = java.util.Vector.new(3)
  VECTOR3.addElement('foo')
  VECTOR3.addElement('bar')
  VECTOR3.addElement('baz')
  
  AOA1x0 = [[]]
  AOA2x3 = [['a', 'b', 'c'], ['d', 'e', 'f']]
  
  VOV1x0 = java.util.Vector.new(1)
  VOV1x0.addElement(java.util.Vector.new(0))
  
  VOV2x3 = java.util.Vector.new(2)
  row = java.util.Vector.new(3)
  row.addElement('a') ; row.addElement('b') ; row.addElement('c')
  VOV2x3.addElement(row)
  row = java.util.Vector.new(3)
  row.addElement('d') ; row.addElement('e') ; row.addElement('f')
  VOV2x3.addElement(row)
end

########################################

class TestArrayToVector < Test::Unit::TestCase
  include Constants4Array2VectorTests
  
  def test_should_create_Vector
    assert_kind_of Java::JavaUtil::Vector, ARRAY0.to_vector, '0'
    assert_kind_of Java::JavaUtil::Vector, ARRAY3.to_vector, '3'
  end
  
  def test_should_contain_correct_elements
    assert VECTOR0.equals(ARRAY0.to_vector), '0'
    assert VECTOR3.equals(ARRAY3.to_vector), '3'
  end
end

########################################

class TestArrayFromVector < Test::Unit::TestCase
  include Constants4Array2VectorTests
  
  def test_should_create_Array_from_Vector
    assert_kind_of Array, Array.from_vector(VECTOR0), '0'
    assert_kind_of Array, Array.from_vector(VECTOR3), '3'
  end
  
  def test_should_contain_correct_elements
    assert_equal ARRAY0, Array.from_vector(VECTOR0), '0'
    assert_equal ARRAY3, Array.from_vector(VECTOR3), '3'
  end
  
  def test_should_work_with_ArrayList
    al3 = java.util.ArrayList.new(3)
    ARRAY3.each { |elem| al3.add(elem) }
    assert_nothing_raised { Array.from_vector(al3) }
    assert_equal ARRAY3, Array.from_vector(al3)
  end
  
  def test_should_work_with_LinkedList
    ll3 = java.util.LinkedList.new
    ARRAY3.each { |elem| ll3.add(elem) }
    assert_nothing_raised { Array.from_vector(ll3) }
    assert_equal ARRAY3, Array.from_vector(ll3)
  end
  
  def test_should_reject_nonAbstractList
    assert_raise(TypeError, 'st') { Array.from_vector(STRING) }
    assert_raise(TypeError, 'hm') { Array.from_vector(HASHTABLE) }
  end
end

########################################

class TestArrayToVoV < Test::Unit::TestCase
  include Constants4Array2VectorTests
  
  def test_should_create_Vector_of_Vectors
    result = AOA1x0.to_vov
    assert_kind_of Java::JavaUtil::Vector, result
    it = result.iterator
    while it.hasNext
      assert_kind_of Java::JavaUtil::Vector, it.next
    end
  end
  
  def test_should_contain_correct_elements
    assert VOV1x0.equals(AOA1x0.to_vov), '1x0'
    assert VOV2x3.equals(AOA2x3.to_vov), '2x3'
  end
end

########################################

class TestArrayFromVectorOfVectors < Test::Unit::TestCase
  include Constants4Array2VectorTests
  
  def test_should_create_AoA_from_VoV
    result = Array.from_vov(VOV1x0)
    assert_kind_of Array, result
    result.each { |row| assert_kind_of Array, row }
  end
  
  def test_should_contain_correct_elements
    assert_equal AOA1x0, Array.from_vov(VOV1x0), '1x0'
    assert_equal AOA2x3, Array.from_vov(VOV2x3), '2x3'
  end
  
  def test_should_reject_nonAbstractList
    assert_raise(TypeError, 'st') { Array.from_vov(STRING) }
    assert_raise(TypeError, 'hm') { Array.from_vov(HASHTABLE) }
  end
  
  def test_should_reject_nonAbstractList_elements
    assert_raise(TypeError) { Array.from_vov(VECTOR3) }
  end
end
